package com.mgm.custom.cheetah;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mgm.base.ConverAdRoot;
import com.mgm.pojo.Advertisement;
import com.mgm.pojo.Creative;
import com.mgm.utils.RandomCodeUtils;
import com.mgm.utils.UUIDUtil;

public class CheetahConverToAdvertisement {
	
	protected static Map<String, String> plmap = new HashMap<>();
	static {
		plmap.put("1", "ios");
		plmap.put("2", "android");
	}
	
	public static JSONArray checkOffer(JSONArray jsonArray) {
		/**
		 * offerid,offername,trackingurl,payouttype,payout,geo,packagename,os／platform
		 */
		Iterator<Object> it = jsonArray.iterator();
		while(it.hasNext()) {
			JSONObject j_obj = (JSONObject) it.next();
			JSONObject j_adSet = j_obj.getJSONArray("adSets").getJSONObject(0);
			JSONObject j_creative = j_adSet.getJSONArray("creatives").getJSONObject(0);
			JSONObject j_capping = j_adSet.getJSONArray("cappings").getJSONObject(0);
			
			int v = 0;
			if(!StringUtils.isEmpty(j_capping.getString("pay_out")) && j_capping.getBigDecimal("pay_out").compareTo(BigDecimal.ZERO)==1 ) { //单价
				v = 1;
			}
			
			if(StringUtils.isEmpty(j_obj.getString("id")) || StringUtils.isEmpty(j_obj.getString("name")) 
					|| StringUtils.isEmpty(j_creative.getString("click_url")) || v==0
					|| StringUtils.isEmpty(j_capping.getString("country")) || StringUtils.isEmpty(j_obj.getString("pkg_name")) 
					|| StringUtils.isEmpty(j_adSet.getString("platform"))) {
				it.remove();
			}
		}
		return jsonArray;
	}
	
	public static ConverAdRoot toAdvertisement(Integer aderId, JSONArray jsonArray) {
		if(jsonArray == null) {
			return null;
		}
		ConverAdRoot root = new ConverAdRoot();
		root.setAdId(aderId);
		Date curTime = new Date();
    	DateTime jodaTime = new DateTime(curTime);
    	DateTime n = jodaTime.plusHours(2);
    	
		int size = jsonArray.size(); //数组大小
		Advertisement adment = null;
		Creative creative = null;
		List<Advertisement> adlist = new ArrayList<>(size);
		Map<String, Advertisement> admap = new HashMap<>(size);
//		Map<String, Creative> ctmap = new HashMap<>(size);
		List<String> adIds = new ArrayList<>(size);
//		List<Creative> clist = new ArrayList<>(size);
		for (int i = 0; i < size; i++) {
			JSONObject j_obj = jsonArray.getJSONObject(i);
			JSONObject j_adSet = j_obj.getJSONArray("adSets").getJSONObject(0);
			
			JSONObject j_creative = j_adSet.getJSONArray("creatives").getJSONObject(0);
			JSONArray j_imagesArr = j_creative.getJSONArray("images");
			JSONObject j_images = j_imagesArr.isEmpty() ? null : j_imagesArr.getJSONObject(0);
			
			JSONObject j_capping = j_adSet.getJSONArray("cappings").getJSONObject(0);
			
			//----广告===========================
			adment = new Advertisement();
//			String offerid = j_obj.getString("id")+RandomCodeUtils.randomNumeric(8); //系统生成offerid
			adment.setAdv_id(aderId); 
//			adment.setOfferId(offerid); 
			adment.setOfferName(j_obj.getString("name"));
			adment.setEnabled(true);
			
			adment.setPay_type("cpi"); //猎豹默认cpi
			adment.setPay_price(j_capping.getBigDecimal("pay_out"));
			adment.setCurrency("USD"); //默认USD
			adment.setRequires(null);
			
			adment.setOs(plmap.get(j_adSet.getString("platform")));
			adment.setCountries(j_capping.getString("country"));
			adment.setClick_url(j_creative.getString("click_url"));
			adment.setCarrier(null);
			adment.setOs_version(j_adSet.getString("min_os_version"));
			adment.setExcitation(null);
    		adment.setExpire_date(n.toDate());
    		adment.setTrackingsolution(null);
    		adment.setKpi(null);
    		
    		adment.setCap_type(null);
    		adment.setConvernsion_cap(null);
    		adment.setDaily_conversion_cap(j_capping.getBigDecimal("daily_install"));
    		adment.setMonth_conversion_cap(null);
    		adment.setPayout_cap(null);
    		adment.setDaily_payout_cap(j_capping.getBigDecimal("daily_cap"));
    		adment.setMonth_payout_cap(null);
    		
    		adment.setAppname(null);
    		adment.setPkgname(j_obj.getString("pkg_name"));
    		adment.setPreview_url(j_obj.getString("preview_url"));
    		
    		adment.setPremium(null);
    		adment.setPremium_price(null);
    		adment.setIcon(j_creative.getString("icon"));
    		adment.setStart_at(curTime);
    		adment.setEnd_at(null);
    		adment.setUpdate_time(curTime);
    		adment.setCounter(0); //默认0
    		adment.setP1(j_obj.getString("id")); //上游offerid
    		
    		//----素材======================
    		if(j_images != null) {
	    		creative = new Creative();
	    		creative.setUid(UUIDUtil.uuidRep());
	//    		creative.setOfferid(offerid);
	    		creative.setName(j_images.getString("creative_name"));
	    		creative.setCountry(j_capping.getString("country"));
	    		creative.setLanguage(null);
	    		creative.setType("image");
	    		creative.setMime(null);
	    		creative.setWidth(j_images.getInteger("width"));
	    		creative.setHeight(j_images.getInteger("height"));
	    		creative.setSize(null);
	    		creative.setUrl(j_images.getString("url"));
	    		creative.setEnabled(true);
//	    		clist.add(creative);
//	    		ctmap.put(adment.getP1(), creative);
    		}
    		
    		adlist.add(adment);
    		admap.put(adment.getP1(), adment);
    		adIds.add(j_obj.getString("id"));
    		
		}
		root.setAdlist(adlist);
		root.setAdmap(admap);
		root.setAdids(adIds);
		
//		root.setCtlist(clist);
//		root.setCtmap(ctmap);
		return root;
	}

}
